/**
 * 发送手机验证码
 * @param mobile 手机号
 */
function ajaxGetCodeToMobile(mobile, that) {
    var url = "/Lvscore_User_M/register/ajaxGetCodeToMobile";
    $.post(url, {
        mobile: mobile
    }, function (data) {
        /* 成功：发送成功
         失败：该手机号已被注册/服务器异常，请稍候重试*/
        $("#get").removeAttr("disabled");
        if (data == 1) {
            alertBox("发送成功,请注意查收");
            settime(that);
        }else if (data == -1){
            //validateSpanMsg("telError","该手机号已被注册");
            setTimeout(function(){
                validateSpanMsg("telError","该手机号已被注册！");
                that.removeAttribute("disabled");
                that.value = "获取验证码";
                that.style.opacity = "1";
            },1000);
        }else{
            setTimeout(function(){
                alertBox('服务器异常，请稍后重试', '');
                that.removeAttribute("disabled");
                that.value = "获取验证码";
                that.style.opacity = "1";
            },1000);
        }
    });
}
//获取验证码
$(function () {
    $("#telphone").val("");
    $("#number").val("");
    //实时监控输入框
    inputChange("#telphone","#telError");
    inputChange("#number","#codeError");
    //function telMesssage(){}
    $("#get").click(function () {
        var tel = $("#telphone").val();
        if (isNotEmpty(tel)) {
            if (isMobile(tel)) {
                $("#get").attr("disabled",true);
                $("#get").val("发送中...");
                ajaxGetCodeToMobile(tel, this);
            } else {
                validateSpanMsg("telError","手机号码格式错误");
            }
        } else {
                validateSpanMsg("telError","请输入手机号");
        }

    })
});

/**
 * 用户注册-填写手机号(表单提交)
 * @param mobile 用户手机号
 * @param code 用户输入验证码
 */
function ajaxRegisterForMobile(mobile, code) {
    var url = "/Lvscore_User_M/register/ajaxRegisterForMobile";
    $.post(url, {
        mobile: mobile,
        code: code
    }, function (data) {
        /*  成功：ok
         失败：验证码输入错误*/
        //console.log(data);
        var checkStatus = $(".checkStatus").is(':checked');
        if (data != 1) {
            validateSpanMsg("codeError","验证码错误");
        } else if (!checkStatus) {
            alertBox("您需要阅读并同意协议", "");
        }
        else if (data == 1 && checkStatus) {
            window.location.href = "/Lvscore_User_M/userWeb/views/register/accounter.html";
            //电话存在cookies
            var tel = $("#telphone").val();
            var cookie = new sweetCookie();
            cookie.setCookie("tel", encodeBase64(tel + ":" + code, 2));
        }
    })
}
function goNextPage() {
    var tel = $("#telphone").val();
    var code = $("#number").val();
    if (isNotEmpty(tel)) {
        if (isMobile(tel)) {
            if (isNotEmpty(code)) {
                if(!/^\d{6}$/.test(code)){
                    validateSpanMsg("codeError","验证码错误");
                }else{
                    ajaxRegisterForMobile(tel, code);
                }
            } else {
                validateSpanMsg("codeError","请输入验证码");
            }
        } else {
            validateSpanMsg("telError","手机号码格式错误");
        }
    } else {
           validateSpanMsg("telError","请输入手机号");
    }

}

